package linuxbox.resource.bean;

import java.util.*;

import linuxbox.exception.*;
import linuxbox.bean.*;
import linuxbox.util.*;

import linuxbox.resource.logic.BoxResourceEntry;

public class BoxResourceEntryBean extends BoxResourceEntry implements ITableBean
{
  protected DbAdapter m_db = null;
  protected boolean m_bTransaction = false;
  static protected String m_TableName = "box_resource";

  public BoxResourceEntryBean(BaseIntKey pk, DbAdapter db)
  {
    this.init(pk, db);
  }

  public BoxResourceEntryBean(BaseIntKey pk)
  {
    this.init(pk, null);
  }

  public void init(BaseIntKey pk, DbAdapter db)
  {
    if (db != null)
    {
      m_db = db;
      m_bTransaction = true;
    }
    if (pk != null)
      setID(pk.getID());
  }

  public static String getTableName()
  {
    return m_TableName;
  }

  static public void setTableName(String str)
  {
    m_TableName = str;
  }

  public void makeConnection() throws Exception
  {
    if ( m_db == null )
      m_db = new DbAdapter(true);
  }

  public void closeConnection() throws Exception
  {
    if (m_db != null)
    {
      m_db.close();
      m_db = null;
    }
  }

  public void load() throws Exception
  {
    StringBuffer sql = new StringBuffer();

    sql.append("select id, name, category, location, description, allowpeople from ");
    sql.append(getTableName());
    sql.append(" where id = " + getID());
    makeConnection();
    m_db.executeQuery(sql.toString());
    if (m_db.next())
    {
    //use the index in the select statement
    //select id, name, category, location, description, allowpeople
      m_iID = m_db.getInt(1);             //id
      m_sName = m_db.getString(2);        //name
      m_iCategory = m_db.getInt(3);       //category
      m_sLocation = m_db.getString(4);    //location
      m_sDescription = m_db.getString(5); //description
      m_iAllowPeople = m_db.getInt(6);    //allowpeople
    }
    else
    {
      if (!m_bTransaction)
        closeConnection();
      throw new LinuxboxException(ExceptionHelper.RESOURCE, "LOAD", "Load error:  m_iID=" + m_iID);
    }
    if (!m_bTransaction)
      closeConnection();
  }

  public void store() throws Exception
  {
    StringBuffer sql = new StringBuffer();
    sql.append("UPDATE ");
    sql.append(getTableName());
    sql.append(" set ");
    DBUtil.setInteger(sql, "category", m_iCategory, true);
    DBUtil.setInteger(sql, "allowpeople", m_iAllowPeople, false);
    DBUtil.setString(sql, "name", m_sName, false);
    DBUtil.setString(sql, "location", m_sLocation, false);
    DBUtil.setString(sql, "description", m_sDescription, false);
    sql.append(" where id = " + m_iID);

//System.out.println(sql);

    makeConnection();
    m_db.executeUpdate(sql.toString());
    if (!m_bTransaction)
      closeConnection();
  }

  public void refresh() throws Exception
  {
    load();
  }

  public void create() throws Exception
  {
    if (m_iID != 0)
    {
      String sql = "INSERT INTO ";
      sql += getTableName();
      sql += " (id) VALUES(" + m_iID + ")";
      makeConnection();
      m_db.executeUpdate(sql);
    }
    else
    {
      String sql = "select max(id) from ";
      sql += getTableName();
      makeConnection();
      m_db.executeQuery(sql);
      if (m_db.next())
      {
        int max_id = m_db.getInt(1);
        max_id++;
        m_iID = max_id;
      }
      else
      {
        m_iID = 1;
      }
      sql = "insert into ";
      sql += getTableName();
      sql += " (id) values(" + m_iID + ")";
      m_db.executeUpdate(sql);
    }
    if (!m_bTransaction)
      closeConnection();
  }

  public void remove() throws Exception
  {
    String sql = "DELETE FROM ";
    sql += getTableName();
    sql += (" where id = " + m_iID);
    makeConnection();
    m_db.executeUpdate(sql);
    if (!m_bTransaction)
      closeConnection();
  }

  public Object findByKey() throws Exception
  {
    StringBuffer sql = new StringBuffer();
    sql.append("select id from ");
    sql.append(getTableName());
    sql.append(" where id = " + m_iID);
    makeConnection();
    m_db.executeQuery(sql.toString());
    BaseIntKey pk = null;
    if(m_db.next())
    {
      pk = new BaseIntKey();
      pk.setID(m_db.getInt(1));
    }
    if (!m_bTransaction)
      closeConnection();
    return pk;
  }

  public int getCount(String sql) throws Exception
  {
    int count = 0;
    try
    {
      makeConnection();
      m_db.executeQuery(sql);
      if (m_db.next())
        count = m_db.getInt(1);
      if (!m_bTransaction)
        closeConnection();
    }
    catch(Exception e)
    {
      if (!m_bTransaction)
        closeConnection();
      throw new LinuxboxException(ExceptionHelper.RESOURCE, "getCount", "Can't getCount: " + e.getMessage());
    }
    return count;
  }

  public Iterator findBySql(String sqlWhere) throws Exception
  {
    Vector v = null;
    try
    {
      StringBuffer sql = new StringBuffer();
      sql.append("select id from ");
      sql.append(getTableName());
      sql.append(sqlWhere);
//System.out.println(sql);
      makeConnection();
      m_db.executeQuery(sql.toString());
      BaseIntKey pk = null;
      v = new Vector();
      while (m_db.next())
      {
        pk = new BaseIntKey();
        pk.setID(m_db.getInt(1));
        v.addElement(pk);
//System.out.println(pk.getID());
      }
      if (!m_bTransaction)
        closeConnection();
    }
    catch(Exception e)
    {
      if (!m_bTransaction)
        closeConnection();
      throw new LinuxboxException(ExceptionHelper.RESOURCE, "FIND", "Can't find record: " + e.getMessage());
    }
    return v.iterator();
  }

  public int getCountBySql(String sqlWhere) throws Exception
  {
    int count = 0;
    try
    {
      StringBuffer sql = new StringBuffer();
      sql.append("select count(id) from ");
      sql.append(getTableName());
      sql.append(sqlWhere);

      makeConnection();
      m_db.executeQuery(sql.toString());
      if (m_db.next())
        count = m_db.getInt(1);
      if (!m_bTransaction)
        closeConnection();
    }
    catch(Exception e)
    {
      if (!m_bTransaction)
        closeConnection();
      throw new LinuxboxException(ExceptionHelper.RESOURCE, "getCountBySql", "Can't find record: " + e.getMessage());
    }
    return count;
  }
}
